package com.gavin.demo4fresco.module.navigation;

import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.gavin.demo4fresco.R;
import com.gavin.demo4fresco.adapters.ThumbnailAdapter;
import com.gavin.demo4fresco.model.entities.PictureObj;
import com.gavin.demo4fresco.widgets.DividerGridItemDecoration;

import java.util.ArrayList;
import java.util.List;

public class ThumbnailDemoActivity extends AppCompatActivity {
    private static final String TAG = ThumbnailDemoActivity.class.getName();
    private Uri mUri = Uri.parse("content://media/external/images/media");
    private RecyclerView mRecycleView;
    private List<PictureObj> objs = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thumbnail_demo);
        setTitle("模拟相册缩略图");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Cursor cursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
                MediaStore.Images.Media.DEFAULT_SORT_ORDER);
        cursor.moveToFirst();
        while (!cursor.isAfterLast()) {
            int ringtoneID = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
            Uri mImageUri = Uri.withAppendedPath(mUri, "" + ringtoneID);
            objs.add(new PictureObj(mImageUri.toString(), mImageUri.toString()));
            cursor.moveToNext();
        }

        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
            cursor.close();
        }

        mRecycleView = (RecyclerView) findViewById(R.id.content_thumbnail_recycle_view);
        mRecycleView.setLayoutManager(new GridLayoutManager(this, 3));

        if (objs.size() > 0) {
            mRecycleView.setAdapter(new ThumbnailAdapter(this, objs));
        }
        mRecycleView.addItemDecoration(new DividerGridItemDecoration(this));
    }
}
